package com.village.friend.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.village.friend.dto.request.UpdateUserDto;
import com.village.friend.dto.response.*;
import com.village.friend.entity.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 参数转换工具
 */
public class ParamUtils {
    public static String aboutPlay = "活地图,攻略王,会游泳,不抽烟,男友力,负责吃,自来熟";
    public static String aboutMe = "女汉子,傻白甜,拖延症,有趣灵魂,双重人格,文艺青年,安静,霸道总裁,大叔控,小萝莉";
    public static String aboutLove = "相对保守,开放,拒绝婚前,跟着感觉走,两情相悦";


    /**
     * 首页 列表 ：数据库 user 转化为 userResDto
     *
     * @param user
     * @return
     */
    public static HomeListUserResDto userToHomeListUserResDto(User user) {
        HomeListUserResDto data = new HomeListUserResDto();
        BeanUtils.copyProperties(user, data, "userId");
        data.setUserId(user.getUid());
        Integer vipLeft = user.getVipLeft();
        Integer vipLevel = user.getVipLevel();
        if (vipLeft <= 0) {
            vipLeft = 0;
        }
        if ((vipLeft != null && vipLeft >= 10000) || (vipLevel != null && vipLevel == 2)) {
            vipLeft = 10000;
        }
        if (StringUtils.isEmpty(data.getCity())) {
            data.setCity("保密");
            data.setDistance(null);
        }
        data.setVipLeft(vipLeft);
        return data;
    }

    /**
     * 首页 获取用户信息：数据库 user 转化为 HomeUserResDto
     *
     * @param user
     * @return
     */
    public static HomeUserResDto userToHomeUserResDto(User user) {
        HomeUserResDto data = new HomeUserResDto();
        BeanUtils.copyProperties(user, data, "username", "photos", "vipLeft", "userId", "flags");
        data.setPhotos(ParamUtils.jsonStrToList(user.getPhotos()));
        Integer vipLeft = user.getVipLeft();
        Integer vipLevel = user.getVipLevel();
        if (vipLeft <= 0) {
            vipLeft = 0;
        }
        if ((vipLeft != null && vipLeft >= 10000) || (vipLevel != null && vipLevel == 2)) {
            vipLeft = 10000;
        }
        if (StringUtils.isEmpty(data.getCity())) {
            data.setCity("保密");
            data.setDistance(null);
        }
        data.setVipLeft(vipLeft);
        data.setUserId(user.getUid());
        data.setFLags(toSelectedFlags(user.getFlags()));
        data.setWeChatAccount(user.getWeChatAccount());
        return data;
    }

    /**
     * 信息： 谁看过我：数据库 user 转化为 UserLookResDto
     *
     * @param user
     * @return
     */
    public static ListUserLookResDto userToListUserLookResDto(User user) {
        ListUserLookResDto data = new ListUserLookResDto();
        BeanUtils.copyProperties(user, data, "lookTime", "userId");
        data.setUserId(user.getUid());
        if (StringUtils.isEmpty(data.getCity())) {
            data.setCity("保密");
        }
        data.setLookTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(user.getUpdateTime()));
        return data;
    }

    /**
     * 我的 更新用户请求 ： UpdateUserDto  转换成 user 实体
     *
     * @param dto
     * @return
     */
    public static User UpdateUserDtoToUser(UpdateUserDto dto) {
        User user = new User();

        if (StringUtils.isNotBlank(dto.getName())) {
            user.setName(dto.getName());
        }
        if (StringUtils.isNotBlank(dto.getIcon())) {
            user.setIcon(dto.getIcon());
        }
        if (StringUtils.isNotBlank(dto.getBirth())) {
            user.setBirth(dto.getBirth());
        }
        if (dto.getHeight() != null) {
            user.setHeight(dto.getHeight());
        }
        if (dto.getWeight() != null) {
            user.setWeight(dto.getWeight());
        }
        if (dto.getPhotos() != null) {
            user.setPhotos(ParamUtils.listToJSONStr(dto.getPhotos()));
        }
        if (dto.getIsHide() != null) {
            user.setIsHide(dto.getIsHide());
        }
        if (dto.getIsLogout() != null) {
            user.setIsLogout(dto.getIsLogout());
        }
        if (dto.getFlags() != null) {
            user.setFlags(ParamUtils.listToStr(dto.getFlags()));
        }
        if (dto.getResidentCity() != null) {
            user.setResidentCity(dto.getResidentCity());
        }
        if (dto.getSelfDesc() != null) {
            user.setSelfDesc(dto.getSelfDesc());
        }
        if (dto.getProfession() != null) {
            user.setProfession(dto.getProfession());
        }
        if (dto.getSalaryYear() != null) {
            user.setSalaryYear(dto.getSalaryYear());
        }
        if (dto.getWeChatAccount() != null) {
            user.setWeChatAccount(dto.getWeChatAccount());
        }

        return user;

    }


    /**
     * 我的 获取用户信息：user 转换为  获取用户信息 UserResDto 实体
     *
     * @param user
     * @return
     */
    public static UserResDto userToUserResDto(User user) {
        UserResDto data = new UserResDto();
        BeanUtils.copyProperties(user, data, "username", "photos", "vipLeft", "userId", "flags");
        data.setPhotos(ParamUtils.jsonStrToList(user.getPhotos()));
        Integer vipLeft = user.getVipLeft();
        Integer vipLevel = user.getVipLevel();
        if (vipLeft <= 0) {
            vipLeft = 0;
        }
        if ((vipLeft != null && vipLeft >= 10000) || (vipLevel != null && vipLevel == 2)) {
            vipLeft = 10000;
        }
        data.setVipLeft(vipLeft);
        data.setFLags(toSelectedFlags(user.getFlags()));
        data.setUserId(user.getUid());
        data.setWeChatAccount(user.getWeChatAccount());
        data.setResidentCity(user.getResidentCity());
        return data;

    }

    /**
     * list 转化为 json字符串，主要用于  photos 的相互转化
     *
     * @param list
     * @return
     */
    public static String listToJSONStr(List list) {
        if (list == null) {
            return "";
        }
        return JSON.toJSONString(list);
    }


    /**
     * json字符串 转化为 list，主要用于  photos 的相互转化
     *
     * @param str
     * @return
     */
    public static List jsonStrToList(String str) {
        if (StringUtils.isNotBlank(str)) {
            return JSONObject.parseObject(str, ArrayList.class);
        } else {
            return new ArrayList();
        }

    }

    /**
     * 用户标签，转换为 用户标签列表
     *
     * @param flags
     * @return
     */
    public static List<FlagResDto> toFlags(String flags) {
        List<FlagResDto> list = new ArrayList<>();
        list.add(toOneFlags(aboutPlay.split(","), flags, "对约玩的要求"));
        list.add(toOneFlags(aboutMe.split(","), flags, "关于我"));
        list.add(toOneFlags(aboutLove.split(","), flags, "对爱的看法"));
        return list;
    }

    /**
     * @param aboutPlayArr
     * @param aboutPlay
     * @param flagName
     * @return
     */
    public static FlagResDto toOneFlags(String[] aboutPlayArr, String aboutPlay, String flagName) {
        FlagResDto aboutPlayDto = new FlagResDto();
        aboutPlayDto.setName(flagName);
        List<ChildResDto> childList = new ArrayList<>();
        for (int i = 0; i < aboutPlayArr.length; i++) {
            ChildResDto ChildDto = new ChildResDto();
            ChildDto.setName(aboutPlayArr[i]);
            if (aboutPlay != null && aboutPlay.contains(aboutPlayArr[i])) {
                ChildDto.setIsSelect(1);
            } else {
                ChildDto.setIsSelect(0);
            }
            childList.add(ChildDto);
        }
        aboutPlayDto.setChild(childList);
        return aboutPlayDto;
    }

    /**
     * 用户标签，转换为 用户标签列表（只转换选择的）
     *
     * @param flags
     * @return
     */
    public static List<FlagResDto> toSelectedFlags(String flags) {
        List<FlagResDto> list = new ArrayList<>();
        list.add(toSelectedOneFlags(aboutPlay.split(","), flags, "对约玩的要求"));
        list.add(toSelectedOneFlags(aboutMe.split(","), flags, "关于我"));
        list.add(toSelectedOneFlags(aboutLove.split(","), flags, "对爱的看法"));
        return list;
    }

    public static FlagResDto toSelectedOneFlags(String[] aboutPlayArr, String aboutPlay, String flagName) {
        FlagResDto aboutPlayDto = new FlagResDto();
        aboutPlayDto.setName(flagName);
        List<ChildResDto> childList = new ArrayList<>();
        for (int i = 0; i < aboutPlayArr.length; i++) {
            ChildResDto ChildDto = new ChildResDto();
            ChildDto.setName(aboutPlayArr[i]);
            if (aboutPlay != null && aboutPlay.contains(aboutPlayArr[i])) {
                ChildDto.setIsSelect(1);
                childList.add(ChildDto);
            }
        }
        aboutPlayDto.setChild(childList);
        return aboutPlayDto;
    }

    /**
     * list 转化为 , 分隔的字符串，主要用于  flags 的相互转化
     *
     * @param list
     * @return
     */
    public static String listToStr(List list) {
        if (list == null || list.size() == 0) {
            return "";
        }
        return StringUtils.join(list, ",");
    }

    /**
     * , 分隔的字符串  转化为 list，主要用于  flags 的相互转化
     *
     * @param str
     * @return
     */
    public static List strToList(String str) {
        if (str == null) {
            return new ArrayList();
        }
        String[] split = str.split(",");
        List<String> list = new ArrayList<>();
        Collections.addAll(list, split);
        return list;
    }

}
